<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入项目名称"
            v-decorator="['itemName', { rules: [{ required: true, message: '请输入项目名称！' }] }]"
          />
        </a-form-item>
        <a-form-item label="项目分数" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number
            :min="0"
            style="width: 100%"
            placeholder="请输入项目分数"
            v-decorator="['fullScore', { rules: [{ required: true, message: ' ' }] }]"
          />
        </a-form-item>
        <a-form-item label="打分项类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group
            v-decorator="['category', { rules: [{ required: true, message: '请输入打分项类型！' }] }]"
            button-style="solid"
          >
            <a-radio-button :value="0">项目打分项</a-radio-button>
            <a-radio-button :value="1">答辩打分项</a-radio-button>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
    <div class="drawer-footer">
      <a-button class="left" @click="handleCancel"> 关闭</a-button>
      <a-button class="right" type="primary" @click="handleSubmit"> 提交</a-button>
    </div>
  </a-drawer>
</template>

<script>
  import { scoreItemAdd, scoreItemEdit } from '@/api/modular/main/scoreItemManage'

  export default {
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        id: null,
        title: null,
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      open(record) {
        if (record) {
          this.id = record.id
          this.title = '编辑打分项'
          setTimeout(() => {
            this.form.setFieldsValue({
              id: record.id,
              itemName: record.itemName,
              fullScore: record.fullScore,
              category: record.category
            })
          }, 500)
        } else {
          setTimeout(() => {
            this.form.setFieldsValue({
              category: 0
            })
          }, 500)
          this.id = ''
          this.title = '新增打分项'
        }
        this.visible = true
      },
      /**
       * 提交表单
       */
      handleSubmit() {
        const {
          form: { validateFields }
        } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof values[key] === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            if (this.id) {
              values = {
                id: this.id,
                ...values
              }
              scoreItemEdit(values)
                .then((res) => {
                  if (res.success) {
                    this.$message.success('编辑成功')
                    this.confirmLoading = false
                    this.$emit('ok', values)
                    this.handleCancel()
                  } else {
                    this.$message.error('编辑失败') //  + res.message
                  }
                })
                .finally((res) => {
                  this.confirmLoading = false
                })
            } else {
              scoreItemAdd(values)
                .then((res) => {
                  if (res.success) {
                    this.$message.success('新增成功')
                    this.confirmLoading = false
                    this.$emit('ok', values)
                    this.handleCancel()
                  } else {
                    this.$message.error('新增失败') // + res.message
                  }
                })
                .finally((res) => {
                  this.confirmLoading = false
                })
            }
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel() {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
